package com.xm.servlet.reception_desk.index;

import com.google.gson.Gson;
import com.xm.service.reception_desk.CommodityService;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 查询商品
 * @author 康文程
 * @date 2024-1-2 上午 11:08
 */
@WebServlet("/product/category")
public class GetProductCategoryServlet extends HttpServlet {
    private final CommodityService service = new CommodityService();
    /**
     * 根据商品类别Id查询商品
     * @param req   an {@link HttpServletRequest} object that
     *                  contains the request the client has made
     *                  of the servlet
     *
     * @param resp  an {@link HttpServletResponse} object that
     *                  contains the response the servlet sends
     *                  to the client
     *
     */
    @SneakyThrows(Exception.class)
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp){
        resp.setContentType("text/html;charset=UTF-8");
        String id = req.getParameter("id");
        Gson gson = new Gson();
        resp.getWriter().println(gson.toJson(service.productCategory(Integer.parseInt(id))));
    }
}
